﻿using MexicoCoffee.Model;
using MexicoCoffee.Printer;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Windows;

namespace MexicoCoffee
{

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static bool isMake = true;
        public static int MakingTypes = 0;
        public static string img_path = "";
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\IPorPortMessage.txt";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var dd = File.ReadAllText(IPorPortUrl);
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.RobotIPRight = porPortInfo.RobotIPRight;
                    IPorPortInfo.Instance.PortName = porPortInfo.PortName;
                    IPorPortInfo.Instance.PrinterCoffeeIP = porPortInfo.PrinterCoffeeIP;
                    IPorPortInfo.Instance.PrinterCofferPort = porPortInfo.PrinterCofferPort;
                    IPorPortInfo.Instance.EWMPortName = porPortInfo.EWMPortName;
                    IPorPortInfo.Instance.ZBPortName = porPortInfo.ZBPortName;
                }
                Thread thr3 = new Thread(SocketClients.Instance.socketclient);
                thr3.IsBackground = true;
                thr3.Start();
                Thread thr = new Thread(IOMsg.Instance.IOSerial_Thr);
                thr.IsBackground = true;
                thr.Start();
                Thread thr1 = new Thread(coffeeMsg.Instance.IOSerial_Thr);
                thr1.IsBackground = true;
                thr1.Start();
                Thread thr4 = new Thread(EWMMsg.Instance.IOSerial_Thr);
                thr4.IsBackground = true;
                thr4.Start();
                Thread thr5 = new Thread(PwebSockets.Instance.Printer_Thr);
                thr5.IsBackground = true;
                thr5.Start();
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 全部订单查询
        /// </summary>
        public static void MessageShow()
        {
            while (true)
            {
                try
                {
                    Device device = new Device();
                    string lists = JsonConvert.SerializeObject(device);
                    var data = HttpMsg.Instance.deviceOrderQueueListMsg(lists);

                    if (data != null)
                    {
                        if (isMake)
                        {
                            var msglist = JsonConvert.DeserializeObject<HTTPMsg>(data);
                            if (msglist != null)
                            {
                                if (msglist.data.ToString().Length > 10)
                                {
                                    var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                    if (list.Count > 0)
                                    {
                                        if (isMake)
                                        {
                                            if (list.Count > 0)
                                            {
                                                var ddd = list.FirstOrDefault(a => a.typeList2.Count > 0);
                                                if (ddd == null) continue;
                                                if (ddd.typeList2.Count > 0)
                                                {
                                                    var make = ddd.typeList2.FirstOrDefault(a => a.status == "3" || a.status == "4");
                                                    if (make != null)
                                                    {
                                                        if (make.jsonCodeVal.Length > 4)
                                                        {
                                                            var coffeecode = JsonConvert.DeserializeObject<List<specification>>(make.jsonCodeVal);
                                                            if (coffeecode.Count > 0)
                                                            {
                                                                var coffeeID = coffeecode.FirstOrDefault(a => a.classCode != "");
                                                                if (coffeeID != null)
                                                                {
                                                                    RobotOperate.CX_Start();
                                                                    Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                                                    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                                                    {
                                                                        //CoffeeMaking(coffeeID.classCode, make, ddd.orderNum);
                                                                        CoffeeMaking("5103", null, "5103");
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception)
                {
                    //return;
                    Thread.Sleep(1000);
                }
            }

        }
        /// <summary>
        /// 取杯
        /// </summary>
        /// <param name="cupCode"></param>
        public static void QBMsg(int cupcode)
        {
            try
            {
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                if (cupcode == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJB1_UP);
                else
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJB2_UP);
                Thread.Sleep(200);
                while (true)
                {
                    Console.WriteLine("daojiebeidian1 " + DateTime.Now.ToString());
                    IOMsg.Instance.RobotBol = false;
                    Thread.Sleep(200);
                    if (cupcode == 2)
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDLBQ1);
                    else
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDLBQ2);
                    Thread.Sleep(200);
                    if (IOMsg.Instance.RobotBol)
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJ_Down);
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KB_Down);
                        Thread.Sleep(200);
                        break;
                    }
                }
                Thread.Sleep(200);
                if (cupcode == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_start);
                else
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup2_start);
                Thread.Sleep(500);
                IOMsg.Instance.AllBol = true;
                if (cupcode == 2)
                {
                    while (WuLiaoCoffeeSel.Instance.LBQFK1_Bol) { Thread.Sleep(10); }
                    while (!WuLiaoCoffeeSel.Instance.LBQFK1_Bol) { Thread.Sleep(10); }
                }
                else
                {
                    while (WuLiaoCoffeeSel.Instance.LBQFK2_Bol) { Thread.Sleep(10); }
                    while (!WuLiaoCoffeeSel.Instance.LBQFK2_Bol) { Thread.Sleep(10); }
                }
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(100);
                if (cupcode == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_end);
                else
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup2_end);
                Thread.Sleep(500);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JCJB_UP);
                Thread.Sleep(500);
                IOMsg.Instance.RobotBol = false;
                while (true)
                {
                    Thread.Sleep(100);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DJBJC);
                    Thread.Sleep(100);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                }
                IOMsg.Instance.AllBol = true;
            }
            catch (Exception)
            {
                return;
            }
        }
        public static int CupCode(string code, int cupCode)
        {
            try
            {
                int coffeecode = 5001;
                switch (code)
                {
                    case "5001":
                        coffeecode = 5001;
                        break;
                    case "5002":
                        if (cupCode == 3)
                            coffeecode = 5024;
                        else
                            coffeecode = 5002;
                        break;
                    case "5003":
                        if (cupCode == 3)
                            coffeecode = 5020;
                        else
                            coffeecode = 5003;
                        break;
                    case "5005":
                        if (cupCode == 3)
                            coffeecode = 5021;
                        else
                            coffeecode = 5005;
                        break;
                    case "5015":
                        if (cupCode == 3)
                            coffeecode = 5016;
                        else
                            coffeecode = 5015;
                        break;
                    case "5017":
                        if (cupCode == 3)
                            coffeecode = 5018;
                        else
                            coffeecode = 5017;
                        break;
                }
                return coffeecode;
            }
            catch (Exception)
            {
                return 5001;
            }
        }
        public static void CoffeeMaking(string makecoffeeName, DeviceOrderQueue list, string orderNum, bool CoffeeBol = true)
        {
            try
            {
                if (!coffeeMsg.Instance.isMaking)
                {
                    isMake = true;
                    return;
                }
                if (!WuLiaoCoffeeSel.Instance.Milk_Sel_Bol || !WuLiaoCoffeeSel.Instance.Water_Sel_Bol)//|| !WuLiaoCoffeeSel.Instance.Water_Sel_Bol || !WuLiaoCoffeeSel.Instance.CupFeed_Sel_Bol
                {
                    isMake = true;
                    return;
                }
                if (!WuLiaoCoffeeSel.Instance.PushRodUP_Sel_Bol || WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol)
                {
                    CoffeeMakes.Instance.MakeMealMsg(orderNum);//出餐
                }
                isMake = false;
                int sugarCode = 0;
                int cupCode = 0;
                var msgs = JsonConvert.DeserializeObject<List<specification>>(list.jsonCodeVal);
                Console.WriteLine(DateTime.Now.ToString() + "  cx10" + list.jsonCodeVal);
                if (msgs != null)
                {
                    if (msgs.Any(a1 => a1.SugarCode != ""))
                    {
                        sugarCode = msgs.Any(a1 => a1.SugarCode == "1") ? 1 : msgs.Any(a1 => a1.SugarCode == "2") ? 2 : msgs.Any(a1 => a1.SugarCode == "3") ? 3 : 0;
                    }
                    if (msgs.Any(b => b.cupCode != ""))
                    {
                        cupCode = msgs.Any(b => b.cupCode == "1") ? 1 : msgs.Any(b => b.cupCode == "2") ? 2 : msgs.Any(b => b.cupCode == "3") ? 3 : 4;
                    }
                }

                EditDeviceOrderStatus editDeviceOrder = new EditDeviceOrderStatus();
                editDeviceOrder.status = 4;
                editDeviceOrder.orderId = list.orderId;
                editDeviceOrder.orderGoodsId = list.id;
                var ddd = JsonConvert.SerializeObject(editDeviceOrder);
                HttpMsg.Instance.editDeviceOrderStatusMsg(ddd);
                Thread.Sleep(1000);
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 10;
                clientList.IsMake = false;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                if (CoffeeBol)
                {
                    IOMsg.Instance.AllBol = false;
                    IOMsg.Instance.RobotBol = false;
                    Thread.Sleep(100);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                    Thread.Sleep(100);
                    if (IOMsg.Instance.RobotBol)
                    {
                        IOMsg.Instance.QK();
                    }
                }
                int code = CupCode(makecoffeeName, cupCode);
                if (CoffeeBol)
                {
                    IOMsg.Instance.AllBol = false;
                    Thread.Sleep(200);
                    if (cupCode == 2)
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJB1_UP);
                    else
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJB2_UP);
                    if (sugarCode == 1 || sugarCode == 2 || sugarCode == 3)
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGT_UP);
                    }
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJ_UP);
                    Thread.Sleep(1000);
                    QBMsg(cupCode);
                    Thread.Sleep(200);
                    IOMsg.Instance.AllBol = true;
                    Thread.Sleep(200);
                    while (true)
                    {
                        if (WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBCG_UP);
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBCG_UP);
                            Thread.Sleep(200);
                            if (sugarCode == 1 || sugarCode == 2 || sugarCode == 3)
                            {
                                //Thread.Sleep(200);
                                //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGT_UP);
                                IOMsg.Instance.RobotBol = false;
                                while (true)
                                {
                                    Thread.Sleep(200);
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDGTD);
                                    Thread.Sleep(200);
                                    if (IOMsg.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                            }
                            if (sugarCode == 1)
                            {
                                IOMsg.Instance.AllBol = false;
                                Thread.Sleep(200);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT1_start);
                                Thread.Sleep(IPorPortInfo.Instance.SugarInit);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT1_end);
                                Thread.Sleep(200);
                                Thread.Sleep(1000);
                                // IOMsg.Instance.AllBol = true;
                            }
                            else if (sugarCode == 2)
                            {
                                IOMsg.Instance.AllBol = false;
                                Thread.Sleep(200);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT2_start);
                                Thread.Sleep(IPorPortInfo.Instance.SugarInit);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT2_end);
                                Thread.Sleep(200);
                                Thread.Sleep(1000);
                            }
                            else if (sugarCode == 3)
                            {
                                IOMsg.Instance.AllBol = false;
                                Thread.Sleep(200);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT3_start);
                                Thread.Sleep(IPorPortInfo.Instance.SugarInit);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT3_end);
                                Thread.Sleep(200);
                                Thread.Sleep(1000);
                            }
                            //if (sugarCode == 1 || sugarCode == 2 || sugarCode == 3)
                            //{
                            //    Thread.Sleep(200);
                            //    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJKF_UP);
                            //    Thread.Sleep(200);
                            //}
                            Console.WriteLine(DateTime.Now.ToString() + "  cx17");
                            Console.WriteLine(DateTime.Now.ToString() + "  cx18");
                            Thread.Sleep(1000);
                            break;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KB_UP);
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JCJB_Down);
                            Thread.Sleep(200);
                            IOMsg.Instance.AllBol = true;
                            Thread.Sleep(200);
                            QBMsg(cupCode);
                            IOMsg.Instance.AllBol = true;
                            Thread.Sleep(200);
                        }
                    }
                }
                Thread.Sleep(1000);
                //CoffeeMakes.Instance.CoffeeMaking(code, list.lhImgPath);
                CoffeeMakes.Instance.CoffeeMaking(code, "");
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(100);
                IOMsg.Instance.RobotBol = false;
                while (true)
                {
                    Thread.Sleep(100);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_CCWC);
                    Thread.Sleep(100);
                    if (IOMsg.Instance.RobotBol)
                    {
                        IOMsg.Instance.QK();
                        Thread.Sleep(200);
                        break;

                    }
                }
                CoffeeMakes.Instance.MakeMealMsg(orderNum);//出餐
                ClientList clientList1 = new ClientList();
                clientList1.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList1.code = 10;
                clientList1.IsMake = false;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList1)));
                IOMsg.Instance.AllBol = true;
                Thread.Sleep(2000);
                GC.Collect();
                EditDeviceOrderStatus editDeviceOrder2 = new EditDeviceOrderStatus();
                editDeviceOrder2.status = 5;
                editDeviceOrder2.orderId = list.orderId;
                editDeviceOrder2.orderGoodsId = list.id;
                var ddd2 = JsonConvert.SerializeObject(editDeviceOrder2);
                HttpMsg.Instance.editDeviceOrderStatusMsg(ddd2);
                isMake = true;
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
